/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HALStateMachines.c
* version:         1.1
* created_by:      Lizette Acosta/Rodrigo Casados
* date_created:    June 22 2013
*=============================================================================*/
/* DESCRIPTION : State Machines                                               */
/*============================================================================*/
/* FUNCTION COMMENT : main function of Window Lifter                          */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.1      | 22/June/2013 |                              | Lizette Acosta   */
/*                                                           Rodrigo Casados  */
/*============================================================================*/
#include "stdtypedef.h"
#include "HALPorts.h"
#include "HALStateMachines.h"
#include "HALDebouncer.h"


static T_UBYTE sub_LedBar_Percentage;

/*============================================================================*/
/*                            vfn_DefineWinStatus                             */
/*============================================================================*/
void vfn_DefineWinStatus(e_StateFlag e_SelectedMode)
{
	if(e_SelectedMode == INITIAL)
	{
		sub_LedBar_Percentage = COMP_CLOSED;
		vfn_RegularWinLif(TO_DEFINE);
	}
}


/*============================================================================*/
/*                             vfn_RegularWinLif                              */
/*============================================================================*/
void vfn_RegularWinLif (e_StateFlag e_SelectedMode)
{
	switch(e_SelectedMode)
	{
		case INITIAL:
			(*ptrToLedEnable[COMP_CLOSED])();
		break;
		
		case TO_DEFINE:
			vfn_ModeSelect();
		break;
		
		case UP:
		
			if(sub_LedBar_Percentage < COMP_CLOSED)		/* Increments only if the window is not completely closed */
			{
				(*ptrToLedEnable[sub_LedBar_Percentage])();
				sub_LedBar_Percentage++;
			}
			
			else
			{
			}
			
		break;
		
		case DOWN:
			if(sub_LedBar_Percentage > COMP_OPEN)		/* Decrements only if the window is not completely open */
			{
				(*ptrToLedEnable[sub_LedBar_Percentage])();
				sub_LedBar_Percentage--;
			}
			
			else
			{
			}
		
		break;
	}
}


/*============================================================================*/
/*                             vfn_PinchMode                                  */
/*============================================================================*/
void vfn_PinchMode(T_UBYTE * u8p_ptrToPinchFlag)
{
	static T_UBYTE slub_5secCont = 0;
	
	if(sub_LedBar_Percentage > COMP_OPEN)				/* Decrements only if the window is not completely open */
		{
			(*ptrToLedEnable[sub_LedBar_Percentage])();
			sub_LedBar_Percentage--;
		}
		
	else
	{
	}
		
	if(slub_5secCont == 5)
	{
		*u8p_ptrToPinchFlag = 0;
	}
	
	else
	{
	}

}



/*============================================================================*/
/*                          vfn_OneTouchFeature                               */
/*============================================================================*/
void vfn_OneTouchFeature(void)
{
	if(sub_LedBar_Percentage < COMP_CLOSED)		/* Increments only if the window is not completely closed */
		{
			(*ptrToLedEnable[sub_LedBar_Percentage])();
			sub_LedBar_Percentage++;
		}
}

/*============================================================================*/
/*                             vfn_ModeSelect                                 */
/*============================================================================*/
void vfn_ModeSelect (void)
{
	static e_StateFlag seub_SelectedMode;
	
	vfn_ReadButtons(&seub_SelectedMode);	/* Reads the 3 buttons and writes the ID of the pressed one. 
									       Writes To_DEFINE if there's no event. */
	switch(seub_SelectedMode)
	{
		case TO_DEFINE:
			
			/* No need to nofity TO_DEFINE status */
			
			break;
			
		case UP:
		case DOWN:
		case PINCH:
		
			vfn_Deboouncer(&seub_SelectedMode);
			/* Possible return values of u8fn_Deboouncer function: */
			/* TO DEFINE: While waiting for debouncer timer to elapse */
			/* INVALID: When it detects an invalid event */
			/* UP/DOWN/PINCH: Upon succesful detection */
			
			if(seub_SelectedMode != TO_DEFINE)
			{
				vfn_RegularWinLif (seub_SelectedMode);
			}
			
			else
			{	/* No need to nofity TO_DEFINE status */
			}
			
			break;
				
		case INVALID:
		
			vfn_RegularWinLif (INVALID);
								
			if(gub_DebouncerFlag) /* In case of mistaken gub_DebouncerFlag activation, */
			{					  /* call vfn_Deboouncer for clearing it and reseting */
								  /* State Machine */
								  
				vfn_Deboouncer(&seub_SelectedMode);
			}
			
			break;
	}
		

	
	
}